package com.bangda.crm.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.bangda.crm.domain.CrmStock;
import com.bangda.crm.domain.vo.ChartVo;
import com.bangda.crm.domain.vo.CrmStockMoveVo;
import com.bangda.crm.service.ICrmStockService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.bangda.crm.domain.CrmStockMove;
import com.bangda.crm.service.ICrmStockMoveService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 库存异动Controller
 * 
 * @author fbs
 * @date 2024-07-11
 */
@RestController
@RequestMapping("/crm/crm_stock_move")
public class CrmStockMoveController extends BaseController
{
    @Autowired
    private ICrmStockMoveService crmStockMoveService;
    @Autowired
    private ICrmStockService crmStockService;

    @GetMapping("/getchart")
    public AjaxResult getchart(){
        List<ChartVo> chart = crmStockMoveService.getChart();
        List<ChartVo> stokeChart = crmStockMoveService.getStokeChart();
        List<ChartVo> chartVos = crmStockService.selectCrmStockListByChart();
        HashMap<String, List<ChartVo>> map = new HashMap<>();
        map.put("outChart",chart);
        map.put("inChart",stokeChart);
        map.put("stokeChart",chartVos);

        return AjaxResult.success(map);
    }
    /**
     * 查询库存异动列表
     */
    @PreAuthorize("@ss.hasPermi('crm:crm_stock_move:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmStockMove crmStockMove)
    {
        startPage();

        if (StringUtils.isNotNull(crmStockMove.getElectricity())){
            Date[] electricity = crmStockMove.getElectricity();
            crmStockMove.setStartTime(electricity[0]);
            crmStockMove.setEndTime(electricity[1]);
        }

        List<CrmStockMove> list = crmStockMoveService.selectCrmStockMoveList(crmStockMove);
        return getDataTable(list);
    }

    /**
     * 导出库存异动列表
     */
    @PreAuthorize("@ss.hasPermi('crm:crm_stock_move:export')")
    @Log(title = "库存异动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmStockMove crmStockMove)
    {
        List<CrmStockMove> list = crmStockMoveService.selectCrmStockMoveList(crmStockMove);
        ExcelUtil<CrmStockMove> util = new ExcelUtil<CrmStockMove>(CrmStockMove.class);
        util.exportExcel(response, list, "库存异动数据");
    }

    /**
     * 获取库存异动详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:crm_stock_move:query')")
    @GetMapping(value = "/{stockMoveId}")
    public AjaxResult getInfo(@PathVariable("stockMoveId") Long stockMoveId)
    {
        return success(crmStockMoveService.selectCrmStockMoveBystockMoveId(stockMoveId));
    }

    /**
     * 新增库存异动
     */
    @PreAuthorize("@ss.hasPermi('crm:crm_stock_move:add')")
    @Log(title = "库存异动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmStockMove crmStockMove)
    {
        CrmStock crmStock = crmStockService.selectCrmStockByStockId(crmStockMove.getStockId());
        crmStockMove.setStockName(crmStock.getStockName());
        if ("进货".equals(crmStockMove.getTradeType())){
            crmStock.setStockSum(crmStock.getStockSum().add(crmStockMove.getQuantity()));
            crmStockService.updateCrmStock(crmStock);

        }else if ("出货".equals(crmStockMove.getTradeType())){

        BigDecimal i=crmStock.getStockSum().subtract(crmStockMove.getQuantity());
        if(i.compareTo(BigDecimal.ZERO)<0){
            return error("库存不足");
        }
        crmStock.setStockSum(crmStock.getStockSum().subtract(crmStockMove.getQuantity()));
        crmStockService.updateCrmStock(crmStock);
        }
        else {
            return error("错误，未选择交易类型");
    }
        return toAjax(crmStockMoveService.insertCrmStockMove(crmStockMove));
    }

    /**
     * 修改库存异动
     */
    @PreAuthorize("@ss.hasPermi('crm:crm_stock_move:edit')")
    @Log(title = "库存异动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmStockMove crmStockMove)
    {
        CrmStock crmStock = crmStockService.selectCrmStockByStockId(crmStockMove.getStockId());
        CrmStockMove crmStockMove1 = crmStockMoveService.selectCrmStockMoveBystockMoveId(crmStockMove.getstockMoveId());
        if (crmStockMove.getTradeType().equals(crmStockMove1.getTradeType())){
            if ("进货".equals(crmStockMove.getTradeType())){
                crmStock.setStockSum(crmStock.getStockSum().subtract(crmStockMove1.getQuantity()).add(crmStockMove.getQuantity()));
                crmStockService.updateCrmStock(crmStock);
                return toAjax(crmStockMoveService.updateCrmStockMove(crmStockMove));
            }

            if ("出货".equals(crmStockMove.getTradeType())){
                BigDecimal subtract = crmStock.getStockSum().add(crmStockMove1.getQuantity()).subtract(crmStockMove.getQuantity());
                if (subtract.compareTo(BigDecimal.ZERO)<0){
                    return error("库存不足");
                }
                crmStock.setStockSum(crmStock.getStockSum().add(crmStockMove1.getQuantity()).subtract(crmStockMove.getQuantity()));
                crmStockService.updateCrmStock(crmStock);
                return toAjax(crmStockMoveService.updateCrmStockMove(crmStockMove));
            }
        }else{
            if ("进货".equals(crmStockMove.getTradeType())){
                crmStock.setStockSum(crmStock.getStockSum().add(crmStockMove1.getQuantity()).add(crmStockMove.getQuantity()));
                crmStockService.updateCrmStock(crmStock);
                return toAjax(crmStockMoveService.updateCrmStockMove(crmStockMove));
            }
            if ("出货".equals(crmStockMove.getTradeType())){
                BigDecimal subtract = crmStock.getStockSum().subtract(crmStockMove1.getQuantity()).subtract(crmStockMove.getQuantity());
                if (subtract.compareTo(BigDecimal.ZERO)<0){
                    return error("库存不足");
                }
                crmStock.setStockSum(subtract);
                crmStockService.updateCrmStock(crmStock);
                return toAjax(crmStockMoveService.updateCrmStockMove(crmStockMove));
            }
        }

        return toAjax(crmStockMoveService.updateCrmStockMove(crmStockMove));
    }

    /**
     * 删除库存异动
     */
    @PreAuthorize("@ss.hasPermi('crm:crm_stock_move:remove')")
    @Log(title = "库存异动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stockMoveIds}")
    public AjaxResult remove(@PathVariable Long[] stockMoveIds)
    {
        for (Long stockMoveId : stockMoveIds) {
            CrmStockMove crmStockMove = crmStockMoveService.selectCrmStockMoveBystockMoveId(stockMoveId);
            if (crmStockMove.getTradeType().equals("进货")){
                CrmStock crmStock = crmStockService.selectCrmStockByStockId(crmStockMove.getStockId());
                crmStock.setStockSum(crmStock.getStockSum().subtract(crmStockMove.getQuantity()));
                BigDecimal i=crmStock.getStockSum().subtract(crmStockMove.getQuantity());
                if(i.compareTo(BigDecimal.ZERO)<0){
                    return error("库存不足");
                }
                crmStockService.updateCrmStock(crmStock);
            }else if (crmStockMove.getTradeType().equals("出货")){
                CrmStock crmStock = crmStockService.selectCrmStockByStockId(crmStockMove.getStockId());
                crmStock.setStockSum(crmStock.getStockSum().add(crmStockMove.getQuantity()));
                crmStockService.updateCrmStock(crmStock);


            }

        }

        return toAjax(crmStockMoveService.deleteCrmStockMoveBystockMoveIds(stockMoveIds));
    }
}
